package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 根据时间段统计营业数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        //获取当天营业额
        Map map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status", Orders.COMPLETED);
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;
        // 有效订单数
        Integer validOrderCount = orderMapper.getOrderByTimeAndStatus(begin,end,Orders.COMPLETED);
        Integer orderCount = orderMapper.getOrderByTimeAndStatus(begin,end,null);

        //新增用户数
        Integer newUsers = userMapper.getUserSum(begin,end);
        //订单完成率

        // 平均客单价
        Double unitPrice = 0.0;
        Double orderCompletionRate = 0.0;
        if(orderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / orderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        return BusinessDataVO
                .builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        //待接单数量2
        Integer waitingOrders = orderMapper.countStatus(Orders.TO_BE_CONFIRMED);

        //待派送数量3
        Integer deliveredOrders = orderMapper.countStatus(Orders.CONFIRMED);

        //已完成数量5
        Integer completedOrders = orderMapper.countStatus(Orders.COMPLETED);
        //已取消数量6
        Integer cancelledOrders = orderMapper.countStatus(Orders.CANCELLED);

        //全部订单
        Integer allOrders = orderMapper.countStatus(null);
        return OrderOverViewVO
                .builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        // 已启售数量
        Integer sold = dishMapper.countByStatus(1);
        // 已停售数量
        Integer discontinued = dishMapper.countByStatus(0);

        return DishOverViewVO
                .builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        // 已启售数量
        Integer sold = setmealMapper.countByStatus(1);

        // 已停售数量
        Integer discontinued = setmealMapper.countByStatus(0);
        return SetmealOverViewVO
                .builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}
